package com.fresh.bus.chat.ctrl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.data.redis.support.collections.RedisZSet;
import org.springframework.stereotype.Controller;

import com.fresh.bus.canoe.command.annotation.CanoeCommand;
import com.fresh.bus.canoe.message.Request;
import com.fresh.bus.canoe.message.Response;
import com.fresh.bus.canoe.session.Session;
import com.fresh.bus.chat.util.MessageUtils;

@Controller("treeCtrl")
public class TreeCtrl extends BaseCtrl {

	@CanoeCommand(value = OpCodes.INIT_IN_WATER)
	public Response initInWater(Session session, Request request) {
		Map<String, String> data = (Map<String, String>) request.getData();
		String userId = session.getRole().getId();
		Response response = new Response(OpCodes.INIT_IN_WATER);
		int time = MessageUtils.getPeriod();
		Map<String, Object> map = new HashMap<String, Object>();
		String waterInfo = this.userService.findUser(userId).get("waterInfo");
		map.put("period", time);
		map.put("waterInfo", waterInfo);
		response.setData(map);
		return response;
	}

	@CanoeCommand(value = OpCodes.REQ_IN_WATER)
	public Response reqInWater(Session session, Request request) {
		Map<String, Object> data = (Map<String, Object>) request.getData();
		Response response = new Response(OpCodes.REQ_IN_WATER);
		int len = (Integer) data.get("len");
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.SECOND, len);
		session.setAttribute("inWaterDoneTime", calendar.getTime());
		session.setAttribute("inWaterTimeLen", len);
		response.setData(len * 1000);
		return response;
	}

	@CanoeCommand(value = OpCodes.FINISH_IN_WATER)
	public Response finishInWater(Session session, Request request) {
		Map<String, Object> data = (Map<String, Object>) request.getData();
		Response response = new Response(OpCodes.FINISH_IN_WATER);
		try {
			String uid = session.getRole().getId();
			int pid = (Integer) data.get("pid");
			int water = this.treeService.gatherWater(uid, pid);
			response.setData(water);
			return response;
		} catch (Exception e) {
			log.error("finishInWater failed", e);
			this.reportError(session);
		}
		return null;
	}

	@CanoeCommand(value = OpCodes.REMOVE_WATER)
	public Response removeWater(Session session, Request request) {
		Map<String, Object> data = (Map<String, Object>) request.getData();
		Response response = new Response(OpCodes.REMOVE_WATER);
		String id = session.getRole().getId();
		int index = (Integer) data.get("index");
		try {
			this.treeService.lostWater(id, index);
			String waterInfo = this.userService.findUser(id).get("waterInfo");
			response.setData(waterInfo);
			return response;
		} catch (Exception e) {
			log.error("removeWater failed", e);
			this.reportError(session);
		}
		return null;
	}

	@CanoeCommand(value = OpCodes.GET_MY_TREE)
	public Response getMyTree(Session session, Request request) {
		Map<String, String> data = (Map<String, String>) request.getData();
		Response response = new Response(OpCodes.GET_MY_TREE);
		String id = session.getRole().getId();
		Map<String, Object> map = this.treeService.getTree(id);
		response.setData(map);
		return response;
	}

	@CanoeCommand(value = OpCodes.GET_TREE_LIST)
	public Response getTreeList(Session session, Request request) {
		Map<String, Object> data = (Map<String, Object>) request.getData();
		try {
			Response response = new Response(OpCodes.GET_TREE_LIST);
			Set<String> trees = this.treeService.trees.reverseRange(0, -1);
			if (trees.size() > 0) {
				response.setData(trees.toArray());
			} else {
				response.setData(false);
			}
			return response;
		} catch (Exception e) {
			log.error("getTreeList failed", e);
			this.reportError(session);
		}
		return null;
	}

	@CanoeCommand(value = OpCodes.READ_TREE)
	public Response readTree(Session session, Request request) {
		Map<String, Object> data = (Map<String, Object>) request.getData();
		try {
			String uid = (String) data.get("u");
			Response response = new Response(OpCodes.READ_TREE);
			Map<String, Object> map = this.treeService.getTree(uid);
			response.setData(map);
			return response;
		} catch (Exception e) {
			log.error("getTreeList failed", e);
			this.reportError(session);
		}
		return null;
	}

	@CanoeCommand(value = OpCodes.WRITE_XINYUAN)
	public Response writeXinYuan(Session session, Request request) {
		Map<String, Object> data = (Map<String, Object>) request.getData();
		Response response = new Response(OpCodes.WRITE_XINYUAN);
		String user = session.getRole().getId();
		String content = (String) data.get("c");
		Map<String, Object> map = this.treeService.writeXinYuan(user, content);
		response.setData(map);
		return response;
	}

	@CanoeCommand(value = OpCodes.OPEN_QINGYI)
	public Response openQingYi(Session session, Request request) {
		Response response = new Response(OpCodes.OPEN_QINGYI);
		String from = session.getRole().getId();
		String to = (String) request.getData();
		this.treeService.openQingYi(from, to);
		response.setData(true);
		return response;
	}

	@CanoeCommand(value = OpCodes.SYN_ITASK)
	public Response readTask(Session session, Request request) {
		Map<String, String> data = (Map<String, String>) request.getData();
		Response response = new Response(OpCodes.SYN_ITASK);
		String user = session.getRole().getId();
		List<Object> list = this.treeService.getITasks(user);
		response.setData(list.toArray());
		return response;
	}

	@CanoeCommand(value = OpCodes.CONFIRM_ITASK)
	public Response confirmITask(Session session, Request request) {
		Map<String, Object> data = (Map<String, Object>) request.getData();
		Response response = new Response(OpCodes.CONFIRM_ITASK);
		String from = session.getRole().getId();
		String to = (String) data.get("u");
		String taskId = (String) data.get("t");
		this.treeService.confirmTask(from, to, taskId);
		response.setData(true);
		return response;
	}

	@CanoeCommand(value = OpCodes.GET_WISH_LIST)
	public Response getWishList(Session session, Request request) {
		Map<String, Object> data = (Map<String, Object>) request.getData();
		Response response = new Response(OpCodes.GET_WISH_LIST);
		String user = session.getRole().getId();
		List<Map<String, Object>> list = this.treeService.getWishs(user);
		response.setData(list.toArray());
		return response;
	}

	@CanoeCommand(value = OpCodes.REQ_PRESENT_WATER)
	public Response reqPresentWater(Session session, Request request) {
		Map<String, Object> data = (Map<String, Object>) request.getData();
		Response response = new Response(OpCodes.REQ_PRESENT_WATER);
		int len = 3;
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.SECOND, len);
		session.setAttribute("presentDoneTime", calendar.getTime());
		session.setAttribute("presentUser", data.get("u"));
		session.setAttribute("presentIndex", data.get("i"));
		response.setData(len * 1000);
		return response;
	}

	// 赠送清水
	@CanoeCommand(value = OpCodes.PRESENT_WATER)
	public Response presentWater(Session session, Request request) {
		Map<String, Object> data = (Map<String, Object>) request.getData();
		Response response = new Response(OpCodes.PRESENT_WATER);
		String from = session.getRole().getId();
		String to = (String) data.get("u");
		int posIndex = (Integer) data.get("i");
		try {
			Map<String, Object> result = this.treeService.exchangeWater(from,
					to, posIndex);
			response.setData(result);
			return response;
		} catch (Exception e) {
			log.error("presentWater failed", e);
			this.reportError(session);
		}
		return null;
	}

	// 开始栽树
	@CanoeCommand(value = OpCodes.INIT_MY_TREE)
	public Response initMyTree(Session session, Request request) {
		Map<String, String> data = (Map<String, String>) request.getData();
		Response response = new Response(OpCodes.INIT_MY_TREE);
		String id = session.getRole().getId();
		Map<String, Object> map = this.treeService.initNewTree(id);
		response.setData(map);
		return response;
	}

	@CanoeCommand(value = OpCodes.REQ_OUT_WATER)
	public Response reqOutWater(Session session, Request request) {
		Map<String, Object> data = (Map<String, Object>) request.getData();
		Response response = new Response(OpCodes.REQ_OUT_WATER);
		int len = 1;
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MINUTE, len);
		// calendar.add(Calendar.SECOND, 30);
		Integer posIndex = (Integer) data.get("water");
		session.setAttribute("outWaterDoneTime", calendar.getTime());
		session.setAttribute("posIndex", posIndex);
		response.setData(len * 60);
		return response;
	}

	@CanoeCommand(value = OpCodes.FINISH_OUT_WATER)
	public Response finishOutWater(Session session, Request request) {
		Map<String, Object> data = (Map<String, Object>) request.getData();
		Response response = new Response(OpCodes.FINISH_OUT_WATER);
		try {
			String userId = session.getRole().getId();
			int posIndex = (Integer) data.get("water");
			String waterType = this.treeService.outWater(userId, posIndex);
			response.setData(Integer.valueOf(waterType));
			return response;
		} catch (Exception e) {
			log.error("finishInWater failed", e);
			this.reportError(session);
		}
		return null;
	}
}
